package cn.itguy.mobileguard.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.itguy.mobileguard.R;
import cn.itguy.mobileguard.SensorHelper;
import cn.itguy.mobileguard.widget.ModeListAdapter;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class ChooseSensorActivity extends BaseActivity {

	private Button   btn_back;
	private Button   btn_confirm;
	private ListView list;
	
	private ModeListAdapter adapter;
	
	private SharedPreferences preferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.choose_sensor);
		
		btn_back    = (Button)   findViewById(R.id.btn_back);
		btn_confirm = (Button)   findViewById(R.id.btn_confirm);
		list        = (ListView) findViewById(android.R.id.list);
		
		btn_back   .setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		
//		preferences = IMGApplication.getInstance().getPreferences();
		
		List<Map<String, Object>> modes = new ArrayList<Map<String, Object>>();
		List<Sensor> sensors = ((SensorManager) getSystemService(SENSOR_SERVICE)).getSensorList(Sensor.TYPE_ALL);
		Map<String, Object> map = null;
		int selectedIndex = 0;
		for (int i = 0; i < sensors.size(); i++) {
			Sensor sensor = sensors.get(i);
			map = new HashMap<String, Object>();
			switch (sensor.getType()) {
			case Sensor.TYPE_ACCELEROMETER:
			case Sensor.TYPE_LIGHT:
				map.put("sensorType", sensor.getType());
				map.put("mode", SensorHelper.sensor2mode(sensor.getType(), this));
				map.put("checked", false);
				if(sensor.getType() == preferences.getInt("sensorType", Sensor.TYPE_ACCELEROMETER)) {
					selectedIndex = modes.size();
					map.put("checked", true);
				}
				modes.add(map);
				break;

			default:
				break;
			}
		}
		
		adapter = new ModeListAdapter();
		adapter.setSelectedItemPos(selectedIndex);
		adapter.setModes(modes);
		list.setAdapter(adapter);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_confirm:
			doSave();
			break;
		}
	}
	
	/**
	 * do save the sensor type work.
	 */
	private void doSave() {
//		MediaPlayer mediaPlayer = IMGApplication.getInstance().getMediaPlayer();
//		if(mediaPlayer != null && mediaPlayer.isPlaying()) {
//			Toast.makeText(this, R.string.tipText_alarm_on, Toast.LENGTH_SHORT).show();
//			return;
//		}
//		preferences.edit().putInt("sensorType", (Integer) ((Map<?, ?>) adapter.getItem(adapter.getSelectedItemPos())).get("sensorType")).commit();
//		Toast.makeText(this, R.string.tipText_mode_success, Toast.LENGTH_SHORT).show();
//		if(checkMyServiceRunningState()) {
//			Intent intent = new Intent(ChooseSensorActivity.this, IGuardService.class);
//			intent.putExtra("changeSensor", true);
//			startService(intent);
//		}
//		finish();
	}

}
